exports.title = "疯狂点击";
exports.description = "先打开想要疯狂点击的app，再打开手替app，打开本脚本，点击执行；本脚本会切换到刚刚打开的app并不断点击\n";
exports.package = "all";
exports.params = [{
    key: "x",
    name: "x位置",
    value: "300"
}, {
    key: "y",
    name: "y位置",
    value: "300"
}];

exports.script = function(shell, param) {
    return {
        TYPE_WINDOW_STATE_CHANGED(data) {
            console.log(data);
            // 窗口切换
            switch (this.flag) {
                case "opening":
                    if (this.flag == "opening" && data.packageName == this.packageName) {
                        console.log("进入应用，开始点击");
                        this.flag = "running";
                        this.pull();
                    }
                    break;
                case "running":
                    this.stop();
                    break;
                default:
                    if (/\.launcher$/.test(data.packageName)) {

                    } else if (["cn.inu1255.threehands", "com.android.systemui"].indexOf(data.packageName) < 0) {
                        console.log("更新最近使用的应用", data.packageName);
                        this.packageName = data.packageName;
                    }
            }
        },
        pull() {
            var that = this;
            var x = param.x;
            var y = param.y;
            var count = 0;
            var run = function() {
                if (that.flag == "running") {
                    console.log("点击", x, y, x, y);
                    shell.swipe(x, y, x, y);
                    setTimeout(run, 20);
                    count++;
                } else {
                    this.flag = "stop";
                    console.log(count, "次");
                }
            };
            run();
        },
        stop() {
            console.log("已停止");
            this.flag = "stop";
        },
        run: function() {
            this.flag = "opening";
            if (this.packageName) {
                shell.open(this.packageName);
            } else {
                shell.toast("请先打开要疯狂点击的目标");
            }
            // shell.doAction(3);
        }
    };
};